<?php

namespace Admin\Controller;

use Think\Controller;

class UserController extends Controller
{


    /***
     * ajax注册
     */
    public function ajax_register()
    {
        $User = M('User');
        if (IS_AJAX) {
            $data['username'] = trim(I('post.username'));
            $data['email'] = I('post.email');
            $data['mobile'] = I('post.mobile');
            $data['password'] = set_password(I('post.password'));
            $check_password = I('post.check_password');

            $username = $User->where("username = '$data[username]'")->find();
            $email = $User->where("email = '$data[email]'")->find();
            $mobile = $User->where("mobile = '$data[mobile]'")->find();

            $patter = "/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/";
            $telephone = "/^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$/";
            $info = array('status' => 1, 'msg' => '注册成功');
            //验证用户，邮箱，手机号码唯一
            if ($username) {
                $info = array('status' => 0, 'msg' => '用户名已存在');
            }
            if ($email) {
                $info = array('status' => 0, 'msg' => '邮箱已存在');
            }
            if ($mobile) {
                $info = array('status' => 0, 'msg' => '手机号码已存在');
            }
            if (!preg_match($patter, $data['email'])) {
                $info = array('status' => 0, 'msg' => '邮箱格式不正确');
            }
            if (!preg_match($telephone, $data['mobile'])) {
                $info = array('status' => 0, 'msg' => '手机号码格式不正确');
            }

            if (strlen($data['username']) < 5) {
                $info = array('status' => 0, 'msg' => '用户名长度不能小于5位');
            }
            if (strlen(I('post.password')) < 6) {
                $info = array('status' => 0, 'msg' => '密码长度不能小于五位');
            }
            if ($check_password != I('post.password')) {
                $info = array('status' => 0, 'msg' => '两次密码输入不一致');
            }
            if ($info['status'] == 1) {
                $User->add($data);
            }
            $this->ajaxReturn($info);

        }
        $this->display();
    }




    /***
     *ajax登录
     */
    public function login()
    {
        $User = M('User');
        $LoginFailed = D('LoginFailed');
        if (IS_POST) {
//            p($_POST);exit;
            $data['username|mobile|email'] = trim(I("post.username"));
            $data['password'] = set_password(I('post.password'));
            $code = I('post.code');
            $remember = I('post.remember');
//            $token = make_token();    //生成token
//            $User->where($data)->setField('token', $token);

            //判断验证码是否正确
//            if (!$this->check_verify($code)) {
//                $this->error('验证码不正确');
//            }
            //有记录，并且错误时间小于1小时，登录次数达到了5次
            if (!$LoginFailed->check_failed_times()) {
                $this->error('您已经登录错误超过5次，请一个小时后再登录');
            }
            //判断用户名和密码是否正确
            $user = $User->where($data)->find();
            if (!$user) {
                $LoginFailed->update_failed_times();
                $this->error('您的账号已被禁用，请联系管理员');
            }
            if (!$user) {

                $this->error('用户名或密码输入错误');

            }
            if ($user['status'] == 0) {
                //判断账号是否被禁用
                $this->error('您的账号已被禁用，请联系管理员');
            }

            if ($remember) {
                setcookie('token', $user['token'], time() + 60 * 60 * 24 * 7, '/');
            } else {
                setcookie('token', $user['token'], null, '/');
            }
            $_SESSION['user'] = $user;
            $this->success('您已成功登录', U('Index/index'));

        } else {
            $this->display();
        }


//        $User = M('User');
//        $LoginFailed = D('LoginFailed');
//        if (IS_AJAX) {
//            $data['username|mobile|email'] = trim(I("post.username"));
//            $data['password'] = set_password(I('post.password'));
//            $code = I('post.code');
//            $remember = I('post.remember');
//            $token = make_token();    //生成token
//            $User->where($data)->setField('token', $token);
//
//            $info = array('status' => 1, 'msg' => '登录成功');
//            //判断验证码是否正确
//            if (!$this->check_verify($code)) {
//                $info = array('status' => 0, 'msg' => '验证码不正确');
//            }
//            //有记录，并且错误时间小于1小时，登录次数达到了5次
//            if (!$LoginFailed->check_failed_times()) {
//
//                $info = array('status' => 0, 'msg' => '登录超过5次');
//            }
//            //判断用户名和密码是否正确
//            $user = $User->where($data)->find();
//            if (!$user) {
//
//                $info = array('status' => 0, 'msg' => '用户名或密码不正确');
//
//            } elseif ($user['status'] == 0) {
//                //判断账号是否被禁用
//                $info = array('status' => 0, 'msg' => '你的账号已禁用');
//            }
//            if (!$user) {
//                $LoginFailed->update_failed_times();
//            }
//            if ($remember) {
//                setcookie('token', $user['token'], time() + 60 * 60 * 24 * 7, '/');
//            } else {
//                setcookie('token', $user['token'], null, '/');
//            }
//
//            $this->ajaxReturn($info);
//        } else {
//            $this->display();
//        }

    }


    /***
     * 验证码
     */
    public function verify()
    {
        $config = array(
            'fontSize' => 30,    // 验证码字体大小
            'length' => 4,     // 验证码位数
            'useNoise' => false, // 关闭验证码杂点
            'codeSet' => '0123456789'
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    /***
     * 检测输入的验证码是否正确，$code为用户输入的验证码字符串
     */

    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
//        return $verify->check($code, $id);
        $this->ajaxReturn($verify->check($code));

    }

    /***
     * 安全退出
     */
    public function logout()
    {
        $_SESSION = array();
        if (isset($_COOKIE[session_name()])) {
            setcookie(session_name(), "", time() - 3600, "/");
        }
        session_destroy();
        $this->success('退出成功', U('User/login'));
    }

    /***
     * 加载无权限页面
     */
    public function no_auth()
    {
        $this->display();
    }
}
